package com.liwei.easyweibo.activity;

import java.io.File;
import java.util.List;

import com.liwei.easyweibo.R;
import com.liwei.easyweibo.api.WeiboShare;
import com.liwei.easyweibo.entity.WeiboContent;
import com.liwei.easyweibo.entity.WeiboItem;
import com.liwei.easyweibo.util.AndroidUtil;
import com.liwei.easyweibo.util.Constant;
import com.liwei.easyweibo.util.ImageLoader;
import com.liwei.easyweibo.util.ImageLoader.NotifiyOver;
import com.liwei.easyweibo.util.Util;
import com.tencent.mm.algorithm.MD5;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 主界面，用于展示微博内容，(可以考虑分享到微信)
 * */
public class HomeActivity extends Activity{
	private static final String TAG = "HomeActivityWeibo";
	private WeiboShare mWeibo;
	private LayoutInflater mInflater;
	
	private ListView mListView;
	private List<WeiboItem> mWeiboItemList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepage);
		mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		initView();
		initData();
	}
	
	public void initView(){
		mListView = (ListView) findViewById(R.id.lv_weibo);
	}
	
	public void initData(){
		if(!Util.isBindToWeibo(this)){
			Intent i = new Intent();
			i.setClass(HomeActivity.this, BindWeiboActivity.class);
			startActivity(i);
		}else{
			mWeibo = new WeiboShare(this);
			String jsonResult = mWeibo.getTencentWeiboContent(0,0,10);
			
			try {
				WeiboContent content = AndroidUtil.parseFormJson(jsonResult);
				mWeiboItemList = content.getmWeiboList();
				
				WeiboItemAdapter adapter = new WeiboItemAdapter();
				mListView.setAdapter(adapter);
				
				adapter.notifyDataSetChanged();
				Util.d("TAG", mWeiboItemList.size()+"");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Util.d(TAG, "ExceptionMsg =  " + e.getMessage());
				e.printStackTrace();
			}
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mWeibo = new WeiboShare(this);
		String jsonResult = mWeibo.getTencentWeiboContent(0,0,10);
		
		try {
			WeiboContent content = AndroidUtil.parseFormJson(jsonResult);
			List<WeiboItem> weiboList = content.getmWeiboList();
			Util.d(TAG, weiboList.size()+"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Util.d(TAG, "ExceptionMsg =  " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	
	public class WeiboItemAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mWeiboItemList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mWeiboItemList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.weibo_item, null);
			}
			LinearLayout layout		 = (LinearLayout) convertView.findViewById(R.id.ll_source_weibo);
			layout.setVisibility(View.GONE);
			
			ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_head_icon);
			TextView weiboContent = (TextView) convertView.findViewById(R.id.tv_weibo_content);
			TextView forwardsNum  = (TextView) convertView.findViewById(R.id.tv_forwards_num);
			TextView commentNum = (TextView) convertView.findViewById(R.id.tv_comment_num);
			TextView fromText			 = (TextView) convertView.findViewById(R.id.tv_from); 
			TextView nickName		 = (TextView) convertView.findViewById(R.id.tv_nick);
			
			//转发的源微博
			ImageView imageView0 = (ImageView) convertView.findViewById(R.id.iv_head_icon0);
			TextView weiboContent0 = (TextView) convertView.findViewById(R.id.tv_weibo_content0);
			TextView forwardsNum0  = (TextView) convertView.findViewById(R.id.tv_forwards_num0);
			TextView commentNum0 = (TextView) convertView.findViewById(R.id.tv_comment_num0);
			TextView fromText0			 = (TextView) convertView.findViewById(R.id.tv_from0); 
			TextView nickName0	 = (TextView) convertView.findViewById(R.id.tv_nick0);
			
			WeiboItem item = (WeiboItem) getItem(position);
			imageView.setImageResource(R.drawable.avator);
			weiboContent.setText(item.getmText());
			forwardsNum.setText("转播("+item.getmCount()+")");
			commentNum.setText("评论("+item.getmMCount()+")");
			fromText.setText("来自 "+item.getmFrom());
			nickName.setText(item.getmNick());
			
			int type = item.getmType();
			if(type == 2){
				//转载的
				layout.setVisibility(View.VISIBLE);
				
				WeiboItem item1 = item.getmWeiboItem();
				imageView0.setImageResource(R.drawable.avator);
				weiboContent0.setText(item1.getmText());
				forwardsNum0.setText("转播("+item1.getmCount()+")");
				commentNum0.setText("评论("+item1.getmMCount()+")");
				fromText0.setText("来自 "+item1.getmFrom());
				nickName0.setText(item1.getmNick());
			}
//				String iconPath = item.getmHead();
//				if(iconPath!= null && "".equals(iconPath)){
//					String ioconPathStr = Util.clearSlash(iconPath);
//					Util.d(TAG, "ioconPathStr = " +ioconPathStr);
//					String name = MD5.getMD5(ioconPathStr) + ioconPathStr.substring(ioconPathStr.lastIndexOf("."));
//					String downloadPath = AndroidUtil.getPrimarySDCard();
//					String filePathStr = downloadPath +Constant.SAVE_IMAGE_PATH+name;
//					//先判断图标有木有，木有了再下载
//					File file = new File(filePathStr);
//					if(file.exists()){
//						Bitmap bitmap = AndroidUtil.getBitmapFromFile(filePathStr, 50, 50);
//						imageView.setImageBitmap(bitmap);
//					}else{
//						//下载图片
//						ImageLoader loader = new ImageLoader(ioconPathStr, notify, HomeActivity.this);
//						
//					}
//				}
			return convertView;
		}
		
		NotifiyOver notify = new NotifiyOver() {
			
			@Override
			public void notifiysuccess(String downloadpath) {
				// TODO Auto-generated method stub
				
			}
		};
	}
}
